package cn.tearcry.jsudoku;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
/*
 * MainFrame.java
 *
 * Created on 
 */

/** 
 *
 * @author  马建乐
 */
public class MainFrame extends javax.swing.JFrame {

    private boolean hightlighter=false;
    private GridButtonFactory factory;
    
    /** Creates new form MainFrame */
    public MainFrame() {
        initComponents();
        //ImageIcon  icon=new ImageIcon(getClass().getResource("/cn/tearcry/jsudoku/Resources/About.png"));   
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/cn/tearcry/jsudoku/Resources/Main.png")));
        
       //addButton();
       factory=new GridButtonFactory(this,gridPanel);
       factory.createButton();       
     setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    //  setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
      Toolkit tool=Toolkit.getDefaultToolkit();
      setLocation(tool.getScreenSize().width/4,tool.getScreenSize().height/8);
      this.setMaximumSize(new Dimension(765,450));
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">//GEN-BEGIN:initComponents
    private void initComponents() {
        toolbar = new javax.swing.JToolBar();
        loadButton = new javax.swing.JButton();
        toolbar.addSeparator();
        saveButton = new javax.swing.JButton();
        restartButton = new javax.swing.JButton();
        undoButton = new javax.swing.JButton();
        redoButton = new javax.swing.JButton();
        hintButton = new javax.swing.JButton();
        exitButton = new javax.swing.JButton();
        gridPanel = new javax.swing.JPanel();
        panelGameInfo = new javax.swing.JPanel();
        lblGameInfo = new javax.swing.JLabel();
        lblHintInfo = new javax.swing.JLabel();
        lblGameWrongInfo = new javax.swing.JLabel();
        gameProgressBar = new javax.swing.JProgressBar();
        panelDifficulty = new javax.swing.JPanel();
        lblDifficulty = new JLabel("",JLabel.CENTER);
        mainMenu = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        newMenu = new javax.swing.JMenu();
        easyItem = new javax.swing.JMenuItem();
        normlItem = new javax.swing.JMenuItem();
        hardItem = new javax.swing.JMenuItem();
        expertItem = new javax.swing.JMenuItem();
        fileSeparator1 = new javax.swing.JSeparator();
        loadItem = new javax.swing.JMenuItem();
        saveItem = new javax.swing.JMenuItem();
        fileSeparator2 = new javax.swing.JSeparator();
        restartItem = new javax.swing.JMenuItem();
        fileSeparator3 = new javax.swing.JSeparator();
        exitItem = new javax.swing.JMenuItem();
        editMenu = new javax.swing.JMenu();
        undoItem = new javax.swing.JMenuItem();
        redoItem = new javax.swing.JMenuItem();
        gameMenu = new javax.swing.JMenu();
        displayCandidatesItem = new javax.swing.JCheckBoxMenuItem();
        showHintItem = new javax.swing.JMenuItem();
        blockWrongMoveItem = new javax.swing.JCheckBoxMenuItem();
        fillAllItem = new javax.swing.JMenuItem();
        lookAndFeelMenu = new javax.swing.JMenu();
        windowsStyleItem = new javax.swing.JMenuItem();
        javaStyleItem = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        helpItem = new javax.swing.JMenuItem();
        aboutItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("CO\u5c0f\u7ec4 - jSudoku 1.0");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        toolbar.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        toolbar.setFloatable(false);
        toolbar.setRollover(true);
        toolbar.setBorderPainted(false);
        loadButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cn/tearcry/jsudoku/Resources/OpenLarge.png")));
        loadButton.setToolTipText("\u6253\u5f00\u4ee5\u524d\u4fdd\u5b58\u7684\u6e38\u620f\u7ee7\u7eed\u8fdb\u884c\u6e38\u620f");
        loadButton.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        loadButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        loadButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileMenuAction(evt);
            }
        });

        toolbar.add(loadButton);

        saveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cn/tearcry/jsudoku/Resources/SaveLarge.png")));
        saveButton.setToolTipText("\u5c06\u5f53\u524d\u8fdb\u884c\u7684\u6e38\u620f\u4fdd\u5b58\u5230\u6587\u4ef6");
        saveButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileMenuAction(evt);
            }
        });

        toolbar.add(saveButton);

        restartButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cn/tearcry/jsudoku/Resources/RestartLarge.png")));
        restartButton.setToolTipText("\u653e\u5f03\u5f53\u524d\u672a\u5b8c\u6210\u7684\u6e38\u620f\uff0c\u91cd\u65b0\u586b\u5199");
        restartButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileMenuAction(evt);
            }
        });

        toolbar.add(restartButton);

        undoButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cn/tearcry/jsudoku/Resources/UndoLarge.png")));
        undoButton.setToolTipText("\u8fd4\u56de\u5230\u4e0a\u4e00\u6b65\u64cd\u4f5c\u524d\u7684\u72b6\u6001");
        undoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editMenuAction(evt);
            }
        });

        toolbar.add(undoButton);

        redoButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cn/tearcry/jsudoku/Resources/RedoLarge.png")));
        redoButton.setToolTipText("\u6062\u590d\u4e0a\u6b21\u7684\u64a4\u6d88\u64cd\u4f5c");
        redoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editMenuAction(evt);
            }
        });

        toolbar.add(redoButton);

        hintButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cn/tearcry/jsudoku/Resources/HintLarge.png")));
        hintButton.setToolTipText("\u83b7\u5f97\u4e0b\u4e00\u6b65\u5e94\u586b\u5199\u7684\u5efa\u8bae");
        hintButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gameMenuAction(evt);
            }
        });

        toolbar.add(hintButton);

        exitButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cn/tearcry/jsudoku/Resources/ExitLarge.png")));
        exitButton.setToolTipText("\u9000\u51fa\u6e38\u620f");
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileMenuAction(evt);
            }
        });

        toolbar.add(exitButton);

        gridPanel.setLayout(new java.awt.GridLayout(3, 3, 2, 2));

        gridPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)));

        panelGameInfo.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        lblGameInfo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        lblHintInfo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblHintInfo.setText(" ");

        lblGameWrongInfo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblGameWrongInfo.setText("        ");

        gameProgressBar.setForeground(new java.awt.Color(255, 153, 51));
        gameProgressBar.setMaximum(0);
        gameProgressBar.setStringPainted(true);

        org.jdesktop.layout.GroupLayout panelGameInfoLayout = new org.jdesktop.layout.GroupLayout(panelGameInfo);
        panelGameInfo.setLayout(panelGameInfoLayout);
        panelGameInfoLayout.setHorizontalGroup(
            panelGameInfoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panelGameInfoLayout.createSequentialGroup()
                .add(lblGameInfo, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(gameProgressBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(lblGameWrongInfo, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(lblHintInfo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 159, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );
        panelGameInfoLayout.setVerticalGroup(
            panelGameInfoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panelGameInfoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                .add(lblGameInfo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 18, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(gameProgressBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 17, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(lblGameWrongInfo)
                .add(lblHintInfo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 17, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );

        panelGameInfoLayout.linkSize(new java.awt.Component[] {gameProgressBar, lblGameInfo, lblGameWrongInfo, lblHintInfo}, org.jdesktop.layout.GroupLayout.VERTICAL);

        panelDifficulty.setLayout(new java.awt.BorderLayout());

        panelDifficulty.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        lblDifficulty.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblDifficulty.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        panelDifficulty.add(lblDifficulty, java.awt.BorderLayout.CENTER);

        fileMenu.setMnemonic('F');
        fileMenu.setText("\u6587\u4ef6(F)");
        fileMenu.setFont(new java.awt.Font("宋体", 0, 13));
        newMenu.setMnemonic('N');
        newMenu.setText("\u65b0\u5efa(N)");
        newMenu.setFont(new java.awt.Font("宋体", 0, 12));
        newMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newMenuActionPerformed(evt);
            }
        });

        easyItem.setText("\u7b80\u5355");
        easyItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startNewPuzzle(evt);
            }
        });

        newMenu.add(easyItem);

        normlItem.setText("\u666e\u901a");
        normlItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startNewPuzzle(evt);
            }
        });

        newMenu.add(normlItem);

        hardItem.setText("\u56f0\u96be");
        hardItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startNewPuzzle(evt);
            }
        });

        newMenu.add(hardItem);

        expertItem.setText("\u4e13\u5bb6");
        expertItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startNewPuzzle(evt);
            }
        });

        newMenu.add(expertItem);

        fileMenu.add(newMenu);

        fileMenu.add(fileSeparator1);

        loadItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.CTRL_MASK));
        loadItem.setFont(new java.awt.Font("宋体", 0, 12));
        loadItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cn/tearcry/jsudoku/Resources/Open.png")));
        loadItem.setMnemonic('L');
        loadItem.setText("\u8bfb\u53d6\u6e38\u620f(L)");
        loadItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileMenuAction(evt);
            }
        });

        fileMenu.add(loadItem);

        saveItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        saveItem.setFont(new java.awt.Font("宋体", 0, 12));
        saveItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cn/tearcry/jsudoku/Resources/Save.png")));
        saveItem.setMnemonic('S');
        saveItem.setText("\u4fdd\u5b58\u6e38\u620f(S)");
        saveItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileMenuAction(evt);
            }
        });

        fileMenu.add(saveItem);

        fileMenu.add(fileSeparator2);

        restartItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F10, 0));
        restartItem.setFont(new java.awt.Font("宋体", 0, 12));
        restartItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cn/tearcry/jsudoku/Resources/Restart.png")));
        restartItem.setMnemonic('C');
        restartItem.setText("\u91cd\u65b0\u5f00\u59cb");
        restartItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileMenuAction(evt);
            }
        });

        fileMenu.add(restartItem);

        fileMenu.add(fileSeparator3);

        exitItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.ALT_MASK));
        exitItem.setFont(new java.awt.Font("宋体", 0, 12));
        exitItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cn/tearcry/jsudoku/Resources/Exit.png")));
        exitItem.setMnemonic('X');
        exitItem.setText("\u9000\u51fa(X)");
        exitItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileMenuAction(evt);
            }
        });

        fileMenu.add(exitItem);

        mainMenu.add(fileMenu);

        editMenu.setMnemonic('E');
        editMenu.setText("\u7f16\u8f91(E)");
        editMenu.setFont(new java.awt.Font("宋体", 0, 13));
        undoItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Z, java.awt.event.InputEvent.CTRL_MASK));
        undoItem.setFont(new java.awt.Font("SansSerif", 0, 12));
        undoItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cn/tearcry/jsudoku/Resources/Undo.png")));
        undoItem.setMnemonic('U');
        undoItem.setText("\u64a4\u6d88(U)");
        undoItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editMenuAction(evt);
            }
        });

        editMenu.add(undoItem);

        redoItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Y, java.awt.event.InputEvent.CTRL_MASK));
        redoItem.setFont(new java.awt.Font("SansSerif", 0, 12));
        redoItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cn/tearcry/jsudoku/Resources/Redo.png")));
        redoItem.setMnemonic('R');
        redoItem.setText("\u6062\u590d(R)");
        redoItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editMenuAction(evt);
            }
        });

        editMenu.add(redoItem);

        mainMenu.add(editMenu);

        gameMenu.setMnemonic('G');
        gameMenu.setText("\u6e38\u620f(G)");
        gameMenu.setFont(new java.awt.Font("宋体", 0, 13));
        displayCandidatesItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F10, 0));
        displayCandidatesItem.setFont(new java.awt.Font("宋体", 0, 12));
        displayCandidatesItem.setText("\u663e\u793a\u5019\u9009\u6570");
        displayCandidatesItem.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                gameMenuStateChanged(evt);
            }
        });

        gameMenu.add(displayCandidatesItem);

        showHintItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F11, 0));
        showHintItem.setFont(new java.awt.Font("宋体", 0, 12));
        showHintItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cn/tearcry/jsudoku/Resources/Hint.png")));
        showHintItem.setMnemonic('H');
        showHintItem.setText("\u63d0\u793a(H)");
        showHintItem.setMargin(new java.awt.Insets(2, 3, 2, 2));
        showHintItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gameMenuAction(evt);
            }
        });

        gameMenu.add(showHintItem);

        blockWrongMoveItem.setFont(new java.awt.Font("宋体", 0, 12));
        blockWrongMoveItem.setSelected(true);
        blockWrongMoveItem.setText("\u963b\u6b62\u9519\u8bef\u79fb\u52a8");
        blockWrongMoveItem.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        blockWrongMoveItem.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                gameMenuStateChanged(evt);
            }
        });

        gameMenu.add(blockWrongMoveItem);

        fillAllItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F12, 0));
        fillAllItem.setFont(new java.awt.Font("宋体", 0, 12));
        fillAllItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cn/tearcry/jsudoku/Resources/FillAll.png")));
        fillAllItem.setText("\u586b\u5145\u6240\u6709");
        fillAllItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gameMenuAction(evt);
            }
        });

        gameMenu.add(fillAllItem);

        lookAndFeelMenu.setText("\u5916\u89c2\u9009\u62e9");
        lookAndFeelMenu.setFont(new java.awt.Font("宋体", 0, 12));
        windowsStyleItem.setFont(new java.awt.Font("宋体", 0, 12));
        windowsStyleItem.setText("Windows\u5916\u89c2");
        windowsStyleItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lookAndFeelAction(evt);
            }
        });

        lookAndFeelMenu.add(windowsStyleItem);

        javaStyleItem.setText("Java\u8de8\u5e73\u53f0\u98ce\u683c");
        javaStyleItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lookAndFeelAction(evt);
            }
        });

        lookAndFeelMenu.add(javaStyleItem);

        gameMenu.add(lookAndFeelMenu);

        mainMenu.add(gameMenu);

        helpMenu.setMnemonic('H');
        helpMenu.setText("\u5e2e\u52a9(H)");
        helpMenu.setFont(new java.awt.Font("宋体", 0, 13));
        helpItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
        helpItem.setFont(new java.awt.Font("宋体", 0, 12));
        helpItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cn/tearcry/jsudoku/Resources/Help.png")));
        helpItem.setMnemonic('C');
        helpItem.setText("\u5e2e\u52a9\u5185\u5bb9(C)");
        helpMenu.add(helpItem);

        aboutItem.setFont(new java.awt.Font("宋体", 0, 12));
        aboutItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cn/tearcry/jsudoku/Resources/About.png")));
        aboutItem.setMnemonic('A');
        aboutItem.setText("\u5173\u4e8e(A)");
        aboutItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutItemActionPerformed(evt);
            }
        });

        helpMenu.add(aboutItem);

        mainMenu.add(helpMenu);

        setJMenuBar(mainMenu);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, gridPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 553, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(panelGameInfo, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(panelDifficulty, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 41, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, toolbar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 519, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(toolbar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(gridPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 426, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(panelDifficulty, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(panelGameInfo, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void lookAndFeelAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lookAndFeelAction
        JMenuItem item=(JMenuItem)evt.getSource();
        if(item==windowsStyleItem)
            setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        else
            setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    }//GEN-LAST:event_lookAndFeelAction

    private void newMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newMenuActionPerformed
// TODO 将在此处添加您的处理代码：
    }//GEN-LAST:event_newMenuActionPerformed

    private void gameMenuStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_gameMenuStateChanged
        JCheckBoxMenuItem item=(JCheckBoxMenuItem)evt.getSource();
        if(item==blockWrongMoveItem)
            factory.setBlockWrongMove(item.isSelected());
        else 
            if(item.isSelected())
                factory.displayCandidates();
            else
                factory.displayOriginalNum();
       
    }//GEN-LAST:event_gameMenuStateChanged

    private void aboutItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutItemActionPerformed
        AboutFrame af=new AboutFrame();
        Toolkit tool=Toolkit.getDefaultToolkit();
        
        int x=(this.getWidth()-af.getWidth())/2;
        int y=(this.getHeight()-af.getHeight())/2;
        af.setLocation(this.getX()+x,this.getY()+y);
        af.setVisible(true);
    }//GEN-LAST:event_aboutItemActionPerformed

    private void gameMenuAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gameMenuAction
      if(evt.getSource() instanceof JMenuItem) {
      JMenuItem item=(JMenuItem)evt.getSource();
        if(item==showHintItem)
            factory.displayHint();
        if(item==fillAllItem)
            factory.fillAll();
      }
      else
          factory.displayHint();
    }//GEN-LAST:event_gameMenuAction

    private void editMenuAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editMenuAction
       if(evt.getSource() instanceof JMenuItem)
       {
           JMenuItem item=(JMenuItem)evt.getSource();
           if(item==undoItem)
               factory.getUndoManager().undo();
          else
               factory.getUndoManager().redo();
       }
       else
       {
            JButton button=(JButton)evt.getSource();
           if(button==undoButton)
               factory.getUndoManager().undo();
            else
               factory.getUndoManager().redo();
       }
    }//GEN-LAST:event_editMenuAction

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        for(int i=0;i<getUndoComponents().length;i++)
            getUndoComponents()[i].setEnabled(false);
        for(int i=0;i<getRedoComponents().length;i++)
            getRedoComponents()[i].setEnabled(false);
        displayCandidatesItem.setEnabled(false);
        updateComponentEnabled(false);
        updateRestartComponent(false);
      
        
    }//GEN-LAST:event_formComponentShown

    private void startNewPuzzle(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startNewPuzzle
        if(evt.getSource()instanceof JMenuItem){
            JMenuItem item=(JMenuItem)evt.getSource();            
            factory.startTheGame();          
           lblDifficulty.setText(item.getText());
        }
    }//GEN-LAST:event_startNewPuzzle

    private void fileMenuAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileMenuAction
        if(evt.getSource() instanceof JMenuItem) {
        JMenuItem item=(JMenuItem)evt.getSource();
        //选择退出时确认
        if(item==exitItem)
            exitProgram();
        else if(item==saveItem)
            factory.saveGame();
        else if(item==loadItem)
            factory.loadGame();
        else
            factory.restartTheGame();
       }
        else {
                JButton button=(JButton)evt.getSource();
                if(button==saveButton)
                    factory.saveGame();
                else  if(button==loadButton)
                    factory.loadGame();
                else if(button==restartButton)
                    factory.restartTheGame();
                else
                    exitProgram();
        }
    }//GEN-LAST:event_fileMenuAction

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        exitProgram();
    }//GEN-LAST:event_formWindowClosing
    
    //退出程序时 确认对话框
    private void exitProgram() {
    int choice=JOptionPane.showConfirmDialog(this,"<html><center>你确定要退出吗？</center></html>",this.getTitle(),JOptionPane.YES_NO_OPTION);
    
     if(JOptionPane.YES_OPTION==choice) {
            dispose();
           
            System.exit(1);            
    }else
        return;
    }
    
    //设置外观属性于为系统支持主题
    private void setLookAndFeel(String lookAndFeelName) {
        try {
            
            UIManager.setLookAndFeel(lookAndFeelName);
             java.awt.Font font  = new  java.awt.Font("宋体",0,12); 
            UIManager.put("OptionPane.font",font);   
            UIManager.put("OptionPane.messageFont",font);
            UIManager.put("OptionPane.buttonFont",font);
            UIManager.put("MenuItem.font",font);
            SwingUtilities.updateComponentTreeUI(this); 
            
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        }
       
    }
    
    public  final JLabel gameInfoLabel() {
        return lblGameInfo;
    }
    public  JLabel gameHintLabel() {
        return lblHintInfo;
    }
    
    public final JLabel gameWrongInfoLabel() {
        return lblGameWrongInfo;
    }
   
    public  Component[] getUndoComponents() {
        Component[] uc=new Component[2];
        uc[0]=undoItem;
        uc[1]=undoButton;
        return uc;
    }
    
    public  Component[] getRedoComponents() {
        Component[] rc=new Component[2];
        rc[0]=redoItem;
        rc[1]=redoButton;
        return rc;
    }
    
    public final JProgressBar getGameProgressBar() {
        return gameProgressBar;
    }
   
    public final JCheckBoxMenuItem getDisplayCandidatesItem() {
        return displayCandidatesItem;
    }
  
    public void updateComponentEnabled(boolean enabled) {
        hintButton.setEnabled(enabled);
        showHintItem.setEnabled(enabled);
        saveItem.setEnabled(enabled);
        saveButton.setEnabled(enabled);
        fillAllItem.setEnabled(enabled);
    }
    
    public void updateRestartComponent(boolean enabled) {
        restartItem.setEnabled(enabled);
        restartButton.setEnabled(enabled);
    }
    
   
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }

    
    
    // 变量声明 - 不进行修改//GEN-BEGIN:variables
    public javax.swing.JMenuItem aboutItem;
    public javax.swing.JCheckBoxMenuItem blockWrongMoveItem;
    public javax.swing.JCheckBoxMenuItem displayCandidatesItem;
    public javax.swing.JMenuItem easyItem;
    public javax.swing.JMenu editMenu;
    public javax.swing.JButton exitButton;
    public javax.swing.JMenuItem exitItem;
    public javax.swing.JMenuItem expertItem;
    public javax.swing.JMenu fileMenu;
    public javax.swing.JSeparator fileSeparator1;
    public javax.swing.JSeparator fileSeparator2;
    public javax.swing.JSeparator fileSeparator3;
    public javax.swing.JMenuItem fillAllItem;
    public javax.swing.JMenu gameMenu;
    public javax.swing.JProgressBar gameProgressBar;
    public javax.swing.JPanel gridPanel;
    public javax.swing.JMenuItem hardItem;
    public javax.swing.JMenuItem helpItem;
    public javax.swing.JMenu helpMenu;
    public javax.swing.JButton hintButton;
    public javax.swing.JMenuItem javaStyleItem;
    public javax.swing.JLabel lblDifficulty;
    public javax.swing.JLabel lblGameInfo;
    public javax.swing.JLabel lblGameWrongInfo;
    public javax.swing.JLabel lblHintInfo;
    public javax.swing.JButton loadButton;
    public javax.swing.JMenuItem loadItem;
    public javax.swing.JMenu lookAndFeelMenu;
    public javax.swing.JMenuBar mainMenu;
    public javax.swing.JMenu newMenu;
    public javax.swing.JMenuItem normlItem;
    public javax.swing.JPanel panelDifficulty;
    public javax.swing.JPanel panelGameInfo;
    public javax.swing.JButton redoButton;
    public javax.swing.JMenuItem redoItem;
    public javax.swing.JButton restartButton;
    public javax.swing.JMenuItem restartItem;
    public javax.swing.JButton saveButton;
    public javax.swing.JMenuItem saveItem;
    public javax.swing.JMenuItem showHintItem;
    public javax.swing.JToolBar toolbar;
    public javax.swing.JButton undoButton;
    public javax.swing.JMenuItem undoItem;
    public javax.swing.JMenuItem windowsStyleItem;
    // 变量声明结束//GEN-END:variables
    
}


